<?php
!defined ('P_W') && exit('Forbidden');
class PW_Evaluations {
	var $house_score = array('1' => '交通','2' => '环境','3' => '规划','4' => '配套','5' => '开发商');
	var $colorArray  = array('1' => 'jt','2' => 'hj','3' => 'gh','4' => 'pt','5' => 'kfs');
	var $fullScore = 100;
	var $overtime = 24;
	var $pw_evaluation_db = null;
	
	function PW_Evaluations($pw_evaluation_db = null){
		$this->__construct($pw_evaluation_db);
	}
	
	function __construct($pw_evaluation_db = null){
		$housefieldsService = $this->getFieldsService();
		$this->house_score = & $housefieldsService->getCompsiteFieldsByType('score');
		$this->pw_evaluation_db = & $pw_evaluation_db;
	}
	
	function getScoreTypeCount(){
		return count((array)$this->house_score);
	}
	
	function getScoreTypeList(){
		return array_keys((array)$this->house_score);
	}
	/**
	 * @return int
	 */
	function getAverageScore(){
		return floor((int)$this->fullScore/5);
	}
	
	/**
	 * 获得综合评分部分代码
	 * @param $hid
	 * @return unknown_type
	 */
	function getScoreHtml($hid){
		$scorehtml = '<ul class="score-list">';
		$scoreinfo = $this->pw_evaluation_db->getScoreByHid($hid);
		$_count = $scoreinfo['scoreinfo']['defindCount'] ? $scoreinfo['scoreinfo']['defindCount'] : $scoreinfo['scoreinfo']['actCount'];
		$_totalScore = $scoreinfo['scoreinfo']['defindAverageScore'] ? $scoreinfo['scoreinfo']['defindAverageScore'] : $scoreinfo['scoreinfo']['actAverageScore'];
		$_totalScore = $_totalScore > $this->fullScore ? $this->fullScore : $_totalScore;
		$ckey = 1;
		foreach ((array)$this->house_score as $key => $value) {
			$_score = 0;
			if ($scoreinfo['scoreinfo'][$key]) {
				$_score = (int)$scoreinfo['scoreinfo'][$key]['act'] + (int)$scoreinfo['scoreinfo'][$key]['def'];
				$_score = $_score > $this->fullScore ? $this->fullScore : $_score;
			}
			$_width = $_score ? floor($_score/$this->fullScore * 256) : 0;
			$scorehtml .= '<li class="'.$this->colorArray[$ckey].'" title="'.$_score.'分"><em>'.$value.'：</em><span class="score-bg"><span style="width: '.$_width.'px;">'.$_score.'分</span></span><span class="scale">'.$_score.'分</span></li>';
			$ckey++;
			if ($ckey > count($this->colorArray)) {
				$ckey = 1;
			}
		}
		$scorehtml .= '</ul>';
		return array($scorehtml,$scoreinfo,$_totalScore,$_count);
	}
	
	/**
	 * 获得评分星级部分代码
	 * @param $hid
	 * @param $uid
	 * @return unknown_type
	 */
	function getStarHtml($hid,$uid){
		$starhtml = '';
		$score = $this->getUserScore($hid,$uid);
		$content = $score['content'] ? unserialize($score['content']) : array();
		$_starmapping = array(1=>"one-star",2=>"two-star",3=>"three-star",4=>"four-star",5=>"five-star");
		$ckey = 1;
		foreach ((array)$this->house_score as $key => $value) {
			$starhtml .= '<div class="rating"><span class="'.$this->colorArray[$ckey].'">'.$value.'：</span><ul class="star-rating" id="'.$key.'_span">';
			$ckey++;
			if ($ckey > count($this->colorArray)) {
				$ckey = 1;
			}
			if ($content[$key]) {
				$start = $content[$key];
				$starhtml .= '<li><a href="javascript:;" class="'.$_starmapping[$start].'-h">#</a></li>';
			} else {
				foreach ($_starmapping as $k => $value) {
					$starhtml .= '<li><a href="javascript:;" class="'.$value.'" id="star_'.$k.'_'.$key.'">'.$k.'</a></li>';
				}
			}
			$starhtml .= '</ul></div>';
		}
		return $starhtml;
	}
	
	/**
	 * 获得用户在超时时间内的评分信息,如果超时则返回空，并且删除该条数据
	 * @param $hid
	 * @param $uid
	 * @return unknown_type
	 */
	function getUserScore($hid,$uid){
		global $timestamp;
		$score = array();
		$score = $this->pw_evaluation_db->getUserScoreByUidHid($uid,$hid);
		if (!empty($score)) {
			if ($score['stime'] <= $timestamp) {
				$this->pw_evaluation_db->clearUserScore($hid,$uid);
			}
		}
		return $score;
	}
	
	/**
	 * 添加用户评分信息
	 * @param $stype	评分类型的KEY
	 * @param $value	分值1-5
	 * @param $hid		楼盘ID
	 * @return unknown_type
	 */
	function setUserScoreInfo($content,$hid,$uid){
		global $timestamp;
		$overtime = $timestamp + (int)$this->overtime * 3600;	//超时时间
		$content = serialize($content);
		$n_score[] = array('hid'		=> $hid,
						   'uid'		=> $uid,
						   'stime'		=> $overtime,
						   'content'	=> $content);
		$this->pw_evaluation_db->replaceUserScore($n_score);
	}
	
	/**
	 * @return the $house_score
	 */
	function getHouse_score() {
		return $this->house_score;
	}

	/**
	 * @return the $colorArray
	 */
	function getColorArray() {
		return $this->colorArray;
	}

	/**
	 * @param $house_score the $house_score to set
	 */
	function setHouse_score($house_score) {
		$this->house_score = $house_score;
	}

	/**
	 * @param $colorArray the $colorArray to set
	 */
	function setColorArray($colorArray) {
		$this->colorArray = $colorArray;
	}
	/**
	 * @return the $overtime
	 */
	function getOvertime() {
		return $this->overtime;
	}

	/**
	 * @param $overtime the $overtime to set
	 */
	function setOvertime($overtime) {
		$this->overtime = $overtime;
	}
	
	function getFieldsService (){
		return House::loadService('HouseFieldsService');
	}
	
}
?>